﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data;
using System.Text;


namespace FCNS.KeepMoneyLibrary.Data
{
    public class TagField
    {
        TagField() { }

        public static void RemoveTag(string tag)
        {
            DataRow r = KMData.DataTableTags.Select("Name = '" + tag + "'")[0];
            if ((int)r["Amout"] > 1)
                r["Amout"] = (int)r["Amout"] - 1;
            else
            {
                KMData.DataTableTags.Rows.Remove(r);
                tags.Remove(tag);
            }
        }

        static List<string> tags = new List<string>();
        public static List<string> GetTagField
        {
            get
            {
                if (tags.Count == 0)
                    foreach (DataRow r in KMData.DataTableTags.Rows)
                        tags.Add((string)r["Name"]);

                return tags;
            }
        }

        public static DataRow NewTag(string tag)
        {
            DataRow r = KMData.DataTableTags.NewRow();
            r["Name"] = tag;
            KMData.DataTableTags.Rows.Add(r);
            tags.Add(tag);

            return r;
        }

        public static void AddTag(List<string> tags)
        {
            foreach (string tag in tags)
            {
                DataRow row;
                DataRow[] rows = KMData.DataTableTags.Select("Name = '" + tag + "'");
                if (rows.Length == 0)
                    row = NewTag(tag);
                else
                    row = rows[0];

                row["Amout"] = (int)row["Amout"] + 1;
            }
        }
    }
}
